<template>
  <el-row :gutter="10">
    <el-col :span="4" class="left">
      <div class="title">
        <span>丨</span>商品列表
      </div><hr>
      <ul class="goods">
        <li v-for="(item,index) in batchData.goods" :key="index" class="li" @click="change(index)">{{ item }}</li>
      </ul>
    </el-col>


    <el-col :span="19" class="right">
      <div class="header"><span>丨</span>溯源批次</div><hr>
      <div class="head">
        <el-button class="button" @click="drawer=true">新增</el-button>
      <el-input class="search"
      :prefix-icon="Search"
      placeholder="请输入搜索批次号"
      v-model="search"
      ></el-input>
    </div>
      <div class="main">
        <div class="table">
          <el-table :data="filterTableData" stripe height="100%">
        <el-table-column prop="id" label="序号"></el-table-column>
        <el-table-column prop="batchNum" label="批次号"></el-table-column>
        <el-table-column prop="num" label="数量"></el-table-column>
        <el-table-column prop="specifications" label="规格"></el-table-column>
        <el-table-column prop="price" label="价格/元"></el-table-column>
        <el-table-column prop="manufactureDate" label="生产日期"></el-table-column>
        <el-table-column prop="area" label="地块"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column prop="" label="操作">
          <template #default="scope">
            <el-button link type="primary"  @click="deleteData(scope.$index)">
            删除
            </el-button>
            <el-button link type="primary"  @click="
            drawer2=true;
            changeIndex=scope.$index;
            originData={...batchData.tableData[scope.$index]};
            for(const key in newData) newData[key]=originData[key];
            ">
            编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
        </div>

        <el-drawer
    v-model="drawer"
    title="新增商品"
    class="drawer"
  >
  <span>序号</span><el-input v-model="insertData.id"></el-input>
  <span>批次号</span><el-input v-model="insertData.batchNum"></el-input>
  <span>数量</span><el-input v-model="insertData.num"></el-input>
  <span>规格</span><el-input v-model="insertData.specifications"></el-input>
  <span>价格/元</span><el-input v-model="insertData.price"></el-input>
  <span>生产日期</span><el-input v-model="insertData.manufactureDate"></el-input>
  <span>地块</span><el-input v-model="insertData.area"></el-input>

  <template #footer>
      <div>
        <el-button type="primary" @click="addData" class="button">增加</el-button>
        <el-button @click="drawer=false" class="button">取消</el-button>
      </div>
    </template>
  </el-drawer>

  <el-drawer
    v-model="drawer2"
    title="编辑商品"
    class="drawer"
  >
  <span>序号</span><el-input v-model="newData.id" :placeholder="originData.id"></el-input>
  <span>批次号</span><el-input v-model="newData.batchNum" :placeholder="originData.batchNum"></el-input>
  <span>数量</span><el-input v-model="newData.num" :placeholder="originData.num"></el-input>
  <span>规格</span><el-input v-model="newData.specifications" :placeholder="originData.specifications"></el-input>
  <span>价格/元</span><el-input v-model="newData.price" :placeholder="originData.price"></el-input>
  <span>生产日期</span><el-input v-model="newData.manufactureDate" :placeholder="originData.manufactureDate"></el-input>
  <span>地块</span><el-input v-model="newData.createTime" :placeholder="originData.createTime"></el-input>
  <template #footer>
      <div>
        <el-button type="primary" @click="changeData" class="button">修改</el-button>
        <el-button @click="drawer2=false" class="button">取消</el-button>
      </div>
    </template>
  </el-drawer>
      </div>
    </el-col>
  </el-row>
</template>

<script setup>
import { onMounted,computed,ref, reactive } from 'vue';
import { useBatchData } from '@/store/Traceability/Traceability';
import { Search } from '@element-plus/icons-vue';

onMounted(()=>{
  const ul=document.querySelectorAll('.goods .li')
  ul[0].classList.add('active')
})

const batchData = useBatchData()
const search = ref('')
const changeIndex = ref()

const drawer = ref(false)
const drawer2 = ref(false)

const insertData = reactive({
  id:'',
  batchNum:'',
  num:'',
  specifications:'',
  price:'',
  manufactureDate:'',
  area:'',
  createTime:'',
})

const originData = reactive({
  id:'',
  batchNum:'',
  num:'',
  specifications:'',
  price:'',
  manufactureDate:'',
  area:'',
  createTime:'',
})
const newData = reactive({
  id:'',
  batchNum:'',
  num:'',
  specifications:'',
  price:'',
  manufactureDate:'',
  area:'',
  createTime:'',
})



function change(index){
    const ul=document.querySelectorAll('.goods .li')
    for(let i = 0;i<ul.length;i++){
      ul[i].classList.remove('active')      
    }    
    ul[index].classList.add('active')
    batchData.goodsChoose = ul[index].innerText
  }
const filterTableData = computed(()=>
   batchData.tableData.filter(data=>!search.value||data.batchNum==search.value)
)

const deleteData=(index)=>{
  batchData.tableData.splice(index, 1)
}

const addData = () =>{
  const today = new Date();
  const year = today.getFullYear();  
  const month = today.getMonth() + 1; 
  const day = today.getDate();       
  insertData.createTime=year+'-'+month+'-'+day
  batchData.tableData.push({...insertData})
  for(const key in insertData){
    insertData[key]=''
  }
  drawer.value=false
}

const changeData = () =>{

for(const key in newData){
  if(newData[key]===''){
    newData[key]=originData[key]
  }
}
batchData.tableData.splice(changeIndex.value,1,{...newData})
for(const key in newData){
  newData[key]=''
}
drawer2.value=false

}
</script>

<style scoped>
@import url('./Batch.css');
</style>